package client.ui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import client.RenrenClient;


public class MainWindow extends JFrame {

	private static final long serialVersionUID = -2133534539333334278L;

	public static final int WIDTH = 300;
	public static final int HEIGHT = 380;
	private final int INIT_LOCA_X = 580;
	private final int INIT_LOCA_Y = 100;

	public static final int HOME = 0;
	public static final int FRIENDS = 1;
	public static final int RECENT = 2;
	public static final int MESSAGE = 3;
	public static final int REMIND = 4;

	public static final Color NAME_COLOR = new Color(79, 98, 40);
	public static final Color NEWS_COLOR = new Color(49, 133, 156);
	public static final Color SCHOOL_COLOR = new Color(182, 109, 49);

	JTabbedPane tabbedPane;
	MyJPanel home_panel;
	MyJPanel friends_panel;
	MyJPanel recent_panel;
	MyJPanel message_panel;
	MyJPanel remind_panel;

	JMenuBar menuBar;
	JMenu account;
	JMenuItem modifyInfo;
	JMenuItem modifyPwd;
	JMenuItem logout;

	private long UID;
	private String nickname;
	private String name;

	public MainWindow(long _uid, String _nickname, String _name) {
		super("RenRenSystem");
		UID = _uid;
		nickname = _nickname;
		name = _name;
		super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		init();
	}

	public void init() {

		this.setBounds(INIT_LOCA_X, INIT_LOCA_Y, WIDTH, HEIGHT);
		this.setResizable(false);
		this.setLayout(null);

		// ===================================左侧标签栏==========================================
		tabbedPane = new JTabbedPane(SwingConstants.LEFT,
				JTabbedPane.SCROLL_TAB_LAYOUT);
		tabbedPane.setVisible(true);
		tabbedPane.setBounds(0, 0, WIDTH - 7, HEIGHT - 55);
		this.add(tabbedPane);

		home_panel = new MyJPanel(HOME, UID, name);
		friends_panel = new MyJPanel(FRIENDS, UID, name);
		recent_panel = new MyJPanel(RECENT, UID, name);
		message_panel = new MyJPanel(MESSAGE, UID, name);
		remind_panel = new MyJPanel(REMIND, UID, name);

		tabbedPane.addTab("首页", home_panel);
		tabbedPane.addTab("好友", friends_panel);
		tabbedPane.addTab("最近来访", recent_panel);
		tabbedPane.addTab("留言", message_panel);
		tabbedPane.addTab("提醒", remind_panel);

		tabbedPane.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				((MyJPanel) tabbedPane.getSelectedComponent()).refresh();
			}
		});
		tabbedPane.setSelectedIndex(0);
		((MyJPanel) tabbedPane.getSelectedComponent()).refresh();

		// ===========================================菜单栏===========================
		modifyInfo = new JMenuItem("修改个人资料");
		modifyInfo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				final JFrame jf = new JFrame();
				jf.setBounds(560, 250, 240, 200);
				jf.setVisible(true);
				jf.setLayout(null);
				jf.setTitle("修改个人资料");

				JLabel nick_name_label = new JLabel("昵称");
				nick_name_label.setBounds(40, 30, 30, 20);
				jf.add(nick_name_label);
				final JFormattedTextField nick_name = new JFormattedTextField();
				nick_name.setBounds(80, 30, 100, 20);
				nick_name.setEditable(true);
				jf.add(nick_name);

				JLabel school_label = new JLabel("学校");
				school_label.setBounds(40, 60, 30, 20);
				jf.add(school_label);
				final JFormattedTextField school = new JFormattedTextField();
				school.setBounds(80, 60, 100, 20);
				jf.add(school);

				JButton confirmButton = new JButton("确认");
				confirmButton.setBounds(40, 100, 60, 30);
				confirmButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {

					}
				});
				jf.add(confirmButton);

				JButton cancelButton = new JButton("取消");
				cancelButton.setBounds(120, 100, 60, 30);
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						jf.setVisible(false);
					}
				});
				jf.add(cancelButton);
			}
		});

		modifyPwd = new JMenuItem("修改密码");
		modifyPwd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				final JFrame jf = new JFrame();
				jf.setBounds(560, 250, 240, 200);
				jf.setVisible(true);
				jf.setLayout(null);
				jf.setTitle("修改密码");

				JLabel origin_pwd_label = new JLabel("旧密码");
				origin_pwd_label.setBounds(20, 30, 50, 20);
				jf.add(origin_pwd_label);
				final JFormattedTextField origin_pwd = new JFormattedTextField();
				origin_pwd.setBounds(80, 30, 100, 20);
				origin_pwd.setEditable(true);
				jf.add(origin_pwd);

				JLabel new_pwd_label = new JLabel("新密码");
				new_pwd_label.setBounds(20, 60, 50, 20);
				jf.add(new_pwd_label);
				final JFormattedTextField new_pwd = new JFormattedTextField();
				new_pwd.setBounds(80, 60, 100, 20);
				jf.add(new_pwd);

				JLabel conf_new_pwd_label = new JLabel("确认新密码");
				conf_new_pwd_label.setBounds(20, 90, 60, 20);
				jf.add(conf_new_pwd_label);
				final JFormattedTextField conf_new_pwd = new JFormattedTextField();
				conf_new_pwd.setBounds(80, 90, 100, 20);
				jf.add(conf_new_pwd);

				JButton confirmButton = new JButton("确认");
				confirmButton.setBounds(40, 120, 60, 30);
				confirmButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {

					}
				});
				jf.add(confirmButton);

				JButton cancelButton = new JButton("取消");
				cancelButton.setBounds(120, 120, 60, 30);
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						jf.setVisible(false);
					}
				});
				jf.add(cancelButton);
			}
		});

		logout = new JMenuItem("注销");
		logout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				RenrenClient.mainFrame.setVisible(false);
				System.exit(1);
			}
		});

		account = new JMenu(nickname);
		account.add(modifyInfo);
		account.add(modifyPwd);
		account.add(logout);

		menuBar = new JMenuBar();
		menuBar.add(account);
		this.setJMenuBar(menuBar);

	}

	public long getUID() {
		return UID;
	}

}
